package com.zonski.jbots.game.entity;

import com.zonski.jbots.engine.*;
import com.zonski.jbots.engine.entity.AbstractMind;
import com.zonski.jbots.engine.entity.MovingEntity;
import com.zonski.jbots.engine.entity.Mind;

import com.zonski.jbots.game.JBotsConstants;
import com.zonski.jbots.game.EntityHelper;

public class InputMind extends AbstractMind
{
    public static final int NORTH = 0;
    public static final int SOUTH = 1;
    public static final int EAST  = 2;
    public static final int WEST  = 3;
    public static final int BOMB  = 4;
    public static final int COMET = 5;
    public static final int BOMB_EGG = 6;
    public static final int BABY_EGG = 7;
    public static final int WORM_EGG = 8;
    public static final int TURRET_EGG = 9;
    public static final int TANK_EGG = 10;

    public static final int MAX_DEFINED_INPUTS = 11;

    private static final int CONCIEVED_SPEED = 100;

    private boolean[] down;
    private boolean[] readAtLeastOnce;

    public InputMind()
    {
        this(MAX_DEFINED_INPUTS);
    }
        

    public InputMind(int maxInputs)
    {
        this.down = new boolean[maxInputs];
        this.readAtLeastOnce = new boolean[maxInputs];
        for(int i=0; i<maxInputs; i++)
        {
            readAtLeastOnce[i] = true;
        }
    }

    public boolean isDown(int inputId)
    {
        boolean b = down[inputId] || !readAtLeastOnce[inputId];
        readAtLeastOnce[inputId] = true;
        return b;
    }

    public void setDown(int inputId, boolean inputDown)
    {
        down[inputId] = inputDown;
        if(inputDown)
        {
            readAtLeastOnce[inputId] = false;
        }
    }

    public void update(Engine engine)
        throws Exception
    {
        if(this.getEntity().getMode() == Modes.DEAD)
        {
            this.getEntity().setRemovable(true);
            this.getEntity().getDevice().play("DeadBot", 1);
            // put down some goodies
            Entity capacity = EntityHelper.addEntity(JBotsConstants.CAPACITY, this.getEntity(), engine, 0);
            EntityHelper.centreAtClosestTile(
                    capacity.bounds, this.getEntity().bounds, engine.getRoom()
            );
            capacity.setMaxCharge(this.getEntity().getMaxCharge()/2);
            return;
        }



        int direction = this.getEntity().direction;
        int speed = 0;

        boolean n = isDown(NORTH);
        boolean s = isDown(SOUTH);
        boolean e = isDown(EAST);
        boolean w = isDown(WEST);
        boolean bomb = isDown(BOMB);
        boolean comet = isDown(COMET);
        boolean bombEgg = isDown(BOMB_EGG);
        boolean turretEgg = isDown(TURRET_EGG);
        boolean wormEgg = isDown(WORM_EGG);
        boolean babyEgg = isDown(BABY_EGG);
        boolean tankEgg = isDown(TANK_EGG);
        if(w)
        {
            speed = CONCIEVED_SPEED;
            direction = Directions.WEST;
        }
        if(e)
        {
            speed = CONCIEVED_SPEED;
            direction = Directions.EAST;
        }
        if(n)
        {
            speed = CONCIEVED_SPEED;
            if(e)
            {
                direction = Directions.NORTH_EAST;
            }else if(w){
                direction = Directions.NORTH_WEST;
            }else{
                direction = Directions.NORTH;
            }
        }
        if(s)
        {
            speed = CONCIEVED_SPEED;
            if(e)
            {
                direction = Directions.SOUTH_EAST;
            }else if(w){
                direction = Directions.SOUTH_WEST;
            }else{
                direction = Directions.SOUTH;
            }
        }
        MovingEntity movingEntity = (MovingEntity)this.getEntity();
        // TODO : we don't really use x and y
        movingEntity.setTargetVelocity(speed);
        movingEntity.setTargetDirection(direction);
        if(speed != 0)
        {
            movingEntity.setMode(Modes.WALKING);
        }else{
            movingEntity.setMode(Modes.NONE);
        }
        UsableResource resource = (UsableResource)movingEntity.getResource(
                JBotsConstants.BOMB);
        if(resource != null)
        {
            if(bomb)
            {
                resource.startUsing(direction);
            }else{
                resource.stopUsing();
            }
        }
        resource = (UsableResource)movingEntity.getResource(
                JBotsConstants.COMET);
        if(resource != null)
        {
            if(comet)
            {
                resource.startUsing(direction);
            }else{
                resource.stopUsing();
            }
        }
        resource = (UsableResource)movingEntity.getResource(
                JBotsConstants.BOMB_BOT);
        if(resource != null)
        {
            if(bombEgg)
            {
                resource.startUsing(direction);
            }else{
                resource.stopUsing();
            }
        }

        resource = (UsableResource)movingEntity.getResource(
                JBotsConstants.TURRET_BOT);
        if(resource != null)
        {
            if(turretEgg)
            {
                resource.startUsing(direction);
            }else{
                resource.stopUsing();
            }
        }

        resource = (UsableResource)movingEntity.getResource(
                JBotsConstants.BABY_BOT);
        if(resource != null)
        {
            if(babyEgg)
            {
                resource.startUsing(direction);
            }else{
                resource.stopUsing();
            }
        }

        resource = (UsableResource)movingEntity.getResource(
                JBotsConstants.CONVERTER_BOT);
        if(resource != null)
        {
            if(wormEgg)
            {
                resource.startUsing(direction);
            }else{
                resource.stopUsing();
            }
        }

        resource = (UsableResource)movingEntity.getResource(
                JBotsConstants.TANK_BOT);
        if(resource != null)
        {
            if(tankEgg)
            {
                resource.startUsing(direction);
            }else{
                resource.stopUsing();
            }
        }
    }

    public Mind copy()
    {
        return new InputMind();
    }
}
